home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_7 / grpoly.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  729 b   |  39 lines  |  [MATF/MATL]

  1. function ppoly(a,b,m,n)
  2. % a is the left  endpoint, input.
  3. % b is the right endpoint, input.
  4. % m is the number of sets of curves, input.
  5. % n is the degree of polynomial approximation, input.
  6. h = (b - a)/m/n;
  7. X = a:h:b;
  8. Y = f(X);
  9. Xp(1) = a;
  10. Yp(1) = f(a);
  11. for k = 1:m,
  12.   Va = X(n*(k-1)+1);
  13.   Vb = X(n*(k)+1);
  14.   VM = ceil(200/m);
  15.   Vh = (Vb-Va)/VM;
  16.   Vab= X(n*(k-1)+1:n*(k)+1);
  17.   Vyy= f(Vab);
  18.   Px = polyfit(Vab,Vyy,n);
  19.   Vx = Va+Vh:Vh:Vb;
  20.   Vy = polyval(Px,Vx);
  21.   Xp = [Xp,Vx];
  22.   Yp = [Yp,Vy];
  23. end
  24. h = (b-a)/200;
  25. X1 = a:h:b;
  26. Y1 = f(X1);
  27. Z = zeros(X);
  28. plot(X,Y,'or',X,Z,'+r',X1,Y1,'-g',Xp,Yp,'-r');
  29. hold on;
  30. h0 = (b - a)/m;
  31. X = a:h0:b;
  32. Y = f(X);
  33. for k = 1:m+1,
  34.   X0 = X(k);
  35.   Y0 = f(X(k));
  36.   plot([X0 X0],[0 Y0],'-r');
  37. end
  38. hold off;
  39.